home *** CD-ROM | disk | FTP | other *** search
/ Roboty / ROBOTS_CD.iso / CD / main.dxr / 00024_Turn Towards Mouse.ls < prev    next >
Encoding:
Text File  |  2005-05-13  |  4.4 KB  |  123 lines

  1. property mySprite, getPDLError, myOrientation, myActivePeriod, myResetFlag, myInitialAngle, myMouseDown
  2.  
  3. on getBehaviorDescription me
  4.   return "TURN TOWARDS MOUSE" & RETURN & RETURN & "Orients a sprite to face (or turn away from) the mouse pointer, even if the sprite is moving." & RETURN & RETURN & "You can choose whether the sprite always turns to face the mouse, or only when the mouse button is up or down. " & "When the mouse is in the other state, the will either return to its initial position, or remain in the position it was in when the mouse state changed." & RETURN & RETURN & "The graphic member is considered to face right when it is at rest." & RETURN & RETURN & "PERMITTED MEMBER TYPES:" & RETURN & "bitmap, Flash, vector shape" & RETURN & RETURN & "PARAMETERS:" & RETURN & "* Whether to turn toward or away from the mouse" & RETURN & "* When to turn (always, while mouse is up, or while mouse is down)" & RETURN & "* Whether to return to initial position or remain in new position"
  5. end
  6.  
  7. on getBehaviorTooltip me
  8.   return "Use with bitmap, Flash and vector shape members." & RETURN & RETURN & "Orients a sprite to face towards or away from the mouse pointer, even if the sprite is moving." & RETURN & RETURN & "You can make the sprite face the mouse at all times, or only when the mouse is up or down. " & "When the mouse is in the other state, the sprite can either revert to its original postion, or remain in the position it was in when the mouse state changed. " & "The member is considered to face right at rest."
  9. end
  10.  
  11. on beginSprite me
  12.   Initialize(me)
  13. end
  14.  
  15. on prepareFrame me
  16.   if Activate(me) then
  17.     Turn(me)
  18.   else
  19.     if myResetFlag then
  20.       if mySprite.rotation <> myInitialAngle then
  21.         mySprite.rotation = myInitialAngle
  22.       end if
  23.     end if
  24.   end if
  25. end
  26.  
  27. on Initialize me
  28.   mySprite = sprite(me.spriteNum)
  29.   memberType = mySprite.member.type
  30.   case memberType of
  31.     #flash, #vectorShape:
  32.       mySprite.scaleMode = #autoSize
  33.   end case
  34.   myMouseDown = the stillDown
  35.   myInitialAngle = mySprite.rotation
  36.   myOrientation = myOrientation = "away from the mouse"
  37.   case myActivePeriod of
  38.     "always":
  39.       myActivePeriod = #always
  40.     "when the mouse is clicked":
  41.       myActivePeriod = #mouseClick
  42.     "when the mouse is released":
  43.       myActivePeriod = #mouseRelease
  44.     "while the mouse is down":
  45.       myActivePeriod = #mouseDown
  46.     "while the mouse is up":
  47.       myActivePeriod = #mouseUp
  48.   end case
  49.   myResetFlag = myResetFlag = "return to the initial position"
  50. end
  51.  
  52. on Activate me
  53.   newStatus = myMouseDown <> the stillDown
  54.   if newStatus then
  55.     myMouseDown = the stillDown
  56.   end if
  57.   case myActivePeriod of
  58.     #always:
  59.       return 1
  60.     #mouseClick:
  61.       return newStatus and the stillDown
  62.     #mouseRelease:
  63.       return newStatus and not (the stillDown)
  64.     #mouseDown:
  65.       return the stillDown
  66.     #mouseUp:
  67.       return not (the stillDown)
  68.   end case
  69. end
  70.  
  71. on Turn me
  72.   if the frameLabel <> "pause" then
  73.     slope = the mouseLoc - mySprite.loc
  74.     angle = GetAngle(me, slope)
  75.     if myOrientation then
  76.       angle = angle + 180
  77.     end if
  78.     if mySprite.rotation <> angle then
  79.       mySprite.rotation = angle
  80.     end if
  81.   end if
  82. end
  83.  
  84. on GetAngle me, slope
  85.   deltaH = slope[1]
  86.   deltaV = slope[2]
  87.   if deltaH then
  88.     slope = float(deltaV) / deltaH
  89.     angle = atan(slope)
  90.     if deltaH < 0 then
  91.       angle = angle + PI
  92.     end if
  93.   else
  94.     if deltaV > 0 then
  95.       angle = PI / 2
  96.     else
  97.       if deltaV < 0 then
  98.         angle = 3 * PI / 2
  99.       else
  100.         angle = 0
  101.       end if
  102.     end if
  103.   end if
  104.   angle = angle * 180 / PI
  105.   return angle
  106. end
  107.  
  108. on isOKToAttach me, aSpriteType, aSpriteNum
  109.   case aSpriteType of
  110.     #graphic:
  111.       return getPos([#bitmap, #flash, #vectorShape], sprite(aSpriteNum).member.type) <> 0
  112.     #script:
  113.       return 0
  114.   end case
  115. end
  116.  
  117. on getPropertyDescriptionList me
  118.   if not (the currentSpriteNum) then
  119.     exit
  120.   end if
  121.   return [#myOrientation: [#comment: "Turn", #format: #string, #range: ["towards the mouse", "away from the mouse"], #default: "towards the mouse"], #myActivePeriod: [#comment: EMPTY, #format: #string, #range: ["while the mouse is down", "while the mouse is up", "when the mouse is clicked", "when the mouse is released", "always"], #default: "while the mouse is down"], #myResetFlag: [#comment: "Otherwise", #format: #string, #range: ["return to the initial position", "remain in the new position"], #default: "return to the initial position"]]
  122. end
  123.